package pw.hais.main.main_app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import pw.hais.main.R;
import pw.hais.main.main_app.app.BaseActivity;
import pw.hais.main.main_app.app.V;
import pw.hais.main.main_app.model.UserModel;
import pw.hais.utils.DownTime;
import pw.hais.utils.SPUtils;

public class StartActivity extends BaseActivity implements View.OnClickListener {
    private Button btn_reg,btn_login;
    private View layout_btn;
    private TextView text_msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        getSupportActionBar().hide();

        initLayoutView();
    }

    private void initLayoutView() {
        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_login = (Button) findViewById(R.id.btn_login);
        layout_btn = findViewById(R.id.layout_btn);
        text_msg = (TextView) findViewById(R.id.text_msg);
        btn_reg.setOnClickListener(this);
        btn_login.setOnClickListener(this);

        app.user = SPUtils.getObject(V.SP.USER, UserModel.class);
        if(app.user!=null){
            layout_btn.setVisibility(View.GONE);
            new DownTime(2*1000,1000){
                @Override
                public void onTick(long millisUntilFinished) {
                    text_msg.setText((int)millisUntilFinished/1000 + "秒后跳到 首页");
                }

                @Override
                public void onFinish() {
                    startActivity(new Intent(context, MainActivity.class));
                    finish();
                }
            }.start();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_reg:
                startActivity(new Intent(context,RegisterActivity.class));
                break;
            case R.id.btn_login:
                startActivityForResult(new Intent(context,LoginActivity.class),100);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode ==100){
            if(app.user!=null)startActivity(new Intent(context, MainActivity.class));
        }
    }
}
